package com.ztom.daily.n2022.m4;

import java.util.HashSet;
import java.util.Set;

/**
 * 山羊拉丁文
 * <p>
 * https://leetcode-cn.com/problems/goat-latin/
 *
 * @author ZhangTao
 */
public class Day21ToGoatLatin {

    public String toGoatLatin(String sentence) {
        Set<Character> vowels = new HashSet<Character>() {{
            add('a');
            add('e');
            add('i');
            add('o');
            add('u');
            add('A');
            add('E');
            add('I');
            add('O');
            add('U');
        }};

        int n = sentence.length();
        int i = 0, cnt = 1;
        StringBuilder res = new StringBuilder();

        while (i < n) {
            int j = i;
            while (j < n && sentence.charAt(j) != ' ') {
                ++j;
            }

            ++cnt;
            if (cnt != 2) {
                res.append(' ');
            }
            if (vowels.contains(sentence.charAt(i))) {
                res.append(sentence, i, j);
            } else {
                res.append(sentence, i + 1, j);
                res.append(sentence.charAt(i));
            }
            res.append('m');
            for (int k = 0; k < cnt; ++k) {
                res.append('a');
            }

            i = j + 1;
        }

        return res.toString();
    }
}
